package cn.xopencode.appletweb.controller.user;


import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import cn.xopencode.userservice.rpc.dept.dto.*;
import cn.xopencode.web.core.util.CommonWebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "部门信息管理")
@RestController
@RequestMapping("/dept")
@Validated
public class DeptController {

    @Autowired
    private HttpServletRequest request;

    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;

    @ApiOperation(value = "查询单个部门信息")
    @GetMapping("/get/{deptId}")
    public CommonResult<DeptDTO> getDept(@RequestBody @PathVariable("deptId") String deptId) {
        return deptRpc.getDept(deptId);
    }

    @ApiOperation(value = "获取父部门下的所有子部门")
    @GetMapping("/get/parent/{parentId}")
    public CommonResult<List<DeptDTO>> getDeptList(@RequestBody @PathVariable("parentId") String parentId) {
        return deptRpc.getDeptList(parentId);
    }

    @ApiOperation(value = "部门列表分页查询")
    @PostMapping("/list")
    public CommonResult<PageResult<DeptDTO>> findDeptList(@RequestBody DeptPageDTO deptPageDTO) {
        return deptRpc.findDeptList(deptPageDTO);
    }

    @ApiOperation(value = "新建部门")
    @PostMapping("/create")
    public CommonResult<String> createDept(@RequestBody CreateDeptDTO createDeptDTO) {
        createDeptDTO.setUserId(CommonWebUtil.getUserId(request));
        return deptRpc.createDept(createDeptDTO);
    }


    @ApiOperation(value = "批量更新部门信息")
    @PostMapping("/update")
    public CommonResult<Integer> updateDept(@RequestBody List<UpdateDeptDTO> updateDeptDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updateDeptDTOList.forEach(updateDeptDTO -> {
            updateDeptDTO.setUserId(userId);
        });
        return deptRpc.updateDept(updateDeptDTOList);
    }

    @ApiOperation(value = "批量更改部门属性")
    @PostMapping("/update/pro")
    public CommonResult<Integer> updateProperty(@RequestBody List<UpdatePropertyDTO> updatePropertyDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updatePropertyDTOList.forEach(updatePropertyDTO -> {
            updatePropertyDTO.setUserId(userId);
        });
        return deptRpc.updateProperty(updatePropertyDTOList);
    }
}
